package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TeachplanServiceImpl implements TeachplanService {

    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;


    @Override
    public List<TeachplanDto> findTeachplanTree(long courseId) {

        return teachplanMapper.selectTreeNodes(courseId);
    }


    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {

        //课程计划id
        Long id = teachplanDto.getId();
        //修改课程计划
        if(id!=null){
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }else{
            //取出同父同级别的课程计划数量
            int count = getTeachplanCount(teachplanDto.getCourseId(), teachplanDto.getParentid());
            Teachplan teachplanNew = new Teachplan();
            //设置排序号
            teachplanNew.setOrderby(count+1);
            BeanUtils.copyProperties(teachplanDto,teachplanNew);

            teachplanMapper.insert(teachplanNew);

        }

    }


    /**
     * @description 获取最新的排序号
     * @param courseId  课程id
     * @param parentId  父课程计划id
     * @return int 最新排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    private int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }

    /**
     * @description 删除课程计划
     * @param id 课程计划id
     * @return void
     * @author Mr.OY
     * @date 2024
     */
    @Override
    public void deleteTeachplan(Long id) {
        Teachplan teachplan1 = teachplanMapper.selectById(id);
        //判断是大章节还是小章节
        if(teachplan1.getGrade()==1){
            //判断该大章节下是否有小章节
            List<Teachplan> teachplan2  = teachplanMapper.selectByParentid(id);
            if(teachplan2==null||teachplan2.size()==0){
                teachplanMapper.deleteById(id);
            }else {
                throw new XueChengPlusException("课程计划信息还有子信息，无法操作");
            }
        }else {
            //删除该小章节下的视频
            teachplanMediaMapper.deleteById(id);
            //删除该小章节
            teachplanMapper.deleteById(id);
        }
    }


    //课程计划排序
    @Override
    public void moveTeachplan(String move, Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        if(move.equals("moveup")){
            teachplan.setOrderby(teachplan.getOrderby()-1);
            teachplanMapper.updateById(teachplan);
        }else {
            teachplan.setOrderby(teachplan.getOrderby()+1);
            teachplanMapper.updateById(teachplan);
        }
    }

}
